home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / graphs / barapp~1.jav < prev    next >
Text File  |  1995-10-31  |  5KB  |  150 lines

  1. /* 
  2.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29. import java.lang.*; 
  30. import java.io.*;
  31. import java.awt.*;
  32. import java.net.*;
  33. import java.applet.*;
  34. import java.util.*;
  35. import Display;
  36.  
  37. /**
  38.  * 
  39.  *
  40.  * @author      Siebe R. Brouwer
  41.  * version      1.1, 13 october 1995
  42.  */
  43.  
  44. public class BarApplet extends Applet {
  45.     protected Display display;
  46.     protected BarDiagram diagram;
  47.     
  48.     public void init() {
  49.     
  50.     String at = getParameter("filename");
  51.     String fileName = (at != null) ? at : "bars.data";
  52.     at = getParameter("dir");
  53.     String dir = (at != null) ? at : "dataFiles/";
  54.     at = getParameter("header");
  55.     String header = (at != null) ? at : "BAR DIAGRAM";
  56.     at = getParameter("xtext");
  57.     String xText = (at != null) ? at : "x-axis";
  58.     at = getParameter("yText");
  59.     String yText = (at != null) ? at : "y-axis";
  60.     at = getParameter("vybegin");
  61.     int vYbegin = (at != null) ? Integer.valueOf(at).intValue() :0;
  62.     at = getParameter("vygap");
  63.     int vYgap = (at != null) ? Integer.valueOf(at).intValue() : 100;
  64.     at = getParameter("xgap");
  65.     int xGap = (at != null) ? Integer.valueOf(at).intValue() : 50;
  66.     at = getParameter("ygap");
  67.         int yGap = (at != null) ? Integer.valueOf(at).intValue() : 50;
  68.         setLayout(new BorderLayout());
  69.     display = new Display(header,xText, yText, xGap, yGap);
  70.     add("Center",display);    
  71.     
  72.         // create a bardiagram with the specific variables
  73.     diagram = new BarDiagram(display,vYbegin,vYgap);
  74.     
  75.         runDiagram(fileName, dir);
  76.     
  77.     }
  78.     // read the input file and generate the diagram
  79.     public void runDiagram(String fileName,String dir) {
  80.     
  81.     String field;
  82.     int state;
  83.     String line;
  84.     int number;
  85.     Vector fields= new Vector();
  86.     
  87.     URL url = new URL(getDocumentBase(), dir + fileName);
  88.     DataInputStream file = new DataInputStream(url.openStream());
  89.     
  90.         line=nextLine(file);
  91.     state = Integer.valueOf(line).intValue(); 
  92.     // read the fields in
  93.     for(int i = 0; i <state;i++) {
  94.         field=nextLine(file);
  95.         fields.addElement(field);
  96.         diagram.addField(field);    
  97.     }
  98.     
  99.     line=nextLine(file);
  100.     state = Integer.valueOf(line).intValue();
  101.     
  102.         // read the index labels with their color
  103.     int nrOfFields = fields.size();
  104.     int c[] = new int[3];
  105.     int d[] = new int[nrOfFields];
  106.     StringTokenizer t;
  107.     for(int i = 0; i < state; i++) {
  108.         String index = nextLine(file);
  109.         line=nextLine(file);
  110.         t= new StringTokenizer(line,",");
  111.         String n=null;
  112.         int j = 0;
  113.         while(t.hasMoreTokens()) {
  114.             n = t.nextToken().trim();
  115.         c[j] = Integer.valueOf(n).intValue();    
  116.         j++;
  117.         }
  118.             line=nextLine(file);
  119.         t = new StringTokenizer(line,",");
  120.             j = 0;
  121.         while(t.hasMoreTokens()) {
  122.         n = t.nextToken().trim();
  123.         d[j] = Integer.valueOf(n).intValue();    
  124.         j++;
  125.         }
  126.         Color color = new Color(c[0], c[1], c[2]);
  127.         diagram.addIndex(index, color);
  128.         for(int k = 0; k <nrOfFields;k++) {
  129.         diagram.addValue(d[k],index,(String)fields.elementAt(k));
  130.         }
  131.     }
  132.     display.drawDiagram(diagram);
  133.     }
  134.      
  135.     /* return the next usable line
  136.      * 
  137.      */
  138.     protected String nextLine(DataInputStream file) {
  139.     String line = file.readLine().trim();
  140.     while(line.startsWith("#") || line.length() == 0) {
  141.         line = file.readLine().trim();
  142.     }
  143.     return line;
  144.     }
  145. }
  146.  
  147.  
  148.  
  149.  
  150.